// main.zig
const std = @import("std");
const tt1 = @import("compbasic.zig");
// ----------------------------------------------------------
// 1. 编译时阶乘
// ----------------------------------------------------------
fn factorial(comptime n: u8) u64 {
    comptime var result: u64 = 1;
    comptime var i: u8 = 1;
    inline while (i <= n) : (i += 1) result *= i;
    return result;
}

// ----------------------------------------------------------
// 2. 编译时生成类型：Point(dimension)
// ----------------------------------------------------------
fn Point(comptime dimensions: comptime_int) type {
    return struct {
        coords: [dimensions]f32,

        pub fn length(self: @This()) f32 {
            var sum: f32 = 0;
            for (self.coords) |c| sum += c * c;
            return @sqrt(sum);
        }
    };
}

// ----------------------------------------------------------
// 3. 编译时字符串哈希
// ----------------------------------------------------------
fn comptimeHash(comptime s: []const u8) u32 {
    comptime var hash: u32 = 0;
    inline for (s) |c| hash = hash * 31 + c;
    return hash;
}

// ----------------------------------------------------------
// 4. 编译时反射结构体字段
// ----------------------------------------------------------
fn printStructFields(comptime T: type) void {
    inline for (@typeInfo(T).@"struct".fields) |field| {
        std.debug.print("  field: {s:<10} type: {s}\n", .{
            field.name,
            @typeName(field.type),
        });
    }
}

const Person = struct {
    name: []const u8,
    age: u8,
};

// ----------------------------------------------------------
// 5. 编译时生成 jump table
// ----------------------------------------------------------
fn dispatch(comptime n: u8, index: u8) u8 {
    comptime var funcs: [n]*const fn (u8) u8 = undefined;
    inline for (0..n) |i| {
        const I = i; // 复制到局部 comptime 变量
        funcs[i] = struct {
            fn f(x: u8) u8 {
                return x + @as(u8, @intCast(I));
            }
        }.f;
    }
    return funcs[index](100);
}

// ----------------------------------------------------------
// main：依次演示
// ----------------------------------------------------------
pub fn main() !void {
    std.debug.print("\n=== 1. 编译时阶乘 ===\n", .{});
    const f5 = factorial(5);
    std.debug.print("5! = {}\n", .{f5});

    std.debug.print("\n=== 2. 编译时生成 Point(3) ===\n", .{});
    const P3 = Point(3);
    const p = P3{ .coords = .{ 1, 2, 3 } };
    std.debug.print("length([1,2,3]) = {d:.2}\n", .{p.length()});

    std.debug.print("\n=== 3. 编译时字符串哈希 ===\n", .{});
    const h = comptimeHash("hello");
    std.debug.print("hash('hello') = 0x{x:0>8}\n", .{h});

    std.debug.print("\n=== 4. 编译时反射结构体 Person ===\n", .{});
    printStructFields(Person);

    std.debug.print("\n=== 5. 编译时 jump table (n=5, index=2) ===\n", .{});
    const r = dispatch(5, 2);
    std.debug.print("dispatch(5,2) = {}  (100+2)\n", .{r});

    std.debug.print("\n=== tt1.tt0 ===\n", .{});
    tt1.tt0();
    // tt1.tt1();
    std.debug.print("\n=== tt1.tt2 ===\n", .{});
    tt1.tt2();
    //tt1.tt3();
}
